{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e5ab74be-3b19-4716-ac1c-c781fbf26194",
   "metadata": {},
   "source": [
    "# 第十节、通用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cad4c422-38c2-4fea-8f9b-84da70446cb5",
   "metadata": {},
   "source": [
    "## 元素级数字函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "953fc9dc-7adc-42fa-954f-a3e23abe9886",
   "metadata": {},
   "source": [
    "- abs(x) 或 absolute(x) - 计算数组中每个元素的绝对值。\n",
    "- sqrt(x) - 计算数组中每个元素的平方根。\n",
    "- square(x) - 计算数组中每个元素的平方。\n",
    "- exp(x) - 计算数组中每个元素的自然指数 e^x。\n",
    "- log(x) - 计算数组中每个元素的自然对数（以 e 为底）。\n",
    "- sin(x) - 计算数组中每个元素的正弦值。\n",
    "- cos(x) - 计算数组中每个元素的余弦值。\n",
    "- tan(x) - 计算数组中每个元素的正切值。\n",
    "- maximum(x1, x2) - 对两个数组中的对应元素取最大值。\n",
    "- minimum(x1, x2) - 对两个数组中的对应元素取最小值。\n",
    "- all(x) - 测试数组中的所有元素是否都为真（非零）。\n",
    "- any(x) - 测试数组中是否有任何元素为真（非零）。\n",
    "- inner(a, b) - 计算两个数组的内积。\n",
    "- clip(a, a_min, a_max) - 将数组中的元素限制在给定的范围内 [a_min, a_max]。\n",
    "- round_(a) 或 around(a) - 四舍五入数组中的每个元素到指定的小数位数。\n",
    "- trace(a) - 返回数组的对角线元素之和。\n",
    "- ceil(x) - 计算数组中每个元素的天花板值，即大于或等于该元素的最小整数。\n",
    "- floor(x) - 计算数组中每个元素的地板值，即小于或等于该元素的最大整数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0b2bb70b-e04f-464b-b8e3-008fc30a0ab7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b98380c8-c3a0-49f9-aed7-659ec720cc49",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.15641516, -1.49772681,  0.75744359, -0.76899718],\n",
       "       [ 0.41043436, -0.79560137, -0.91065117, -2.88503685],\n",
       "       [-0.02840927, -0.49558449, -1.86426955, -0.68351367]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randn(3, 4)\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "832e638e-e2f8-4e49-b777-d28df239d9ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.15641516, 1.49772681, 0.75744359, 0.76899718],\n",
       "       [0.41043436, 0.79560137, 0.91065117, 2.88503685],\n",
       "       [0.02840927, 0.49558449, 1.86426955, 0.68351367]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.abs(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "fd743204-98f1-4f3b-8d08-47c9010990a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2, -1, -3],\n",
       "       [-5, -5,  1]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.random.randint(-5, 5, size=(2, 3))\n",
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "06e3e42b-fa63-4fbf-9782-45dd8a12f493",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.41421356, 1.        , 1.73205081],\n",
       "       [2.23606798, 2.23606798, 1.        ]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算数组中每个元素的平方根。\n",
    "np.sqrt(np.abs(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "856ee8af-9a54-4284-a892-ba84573288de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 4,  1,  9],\n",
       "       [25, 25,  1]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算数组中每个元素的平方。\n",
    "np.square(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f2c25490-0338-48f0-8e57-2ece88b40862",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.13533528, 0.36787944, 0.04978707],\n",
       "       [0.00673795, 0.00673795, 2.71828183]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算数组中每个元素的自然指数 e^x\n",
    "np.exp(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "81d8c47f-d2a3-4844-a9d6-9107bbbd889a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.38629436, 0.        , 2.19722458],\n",
       "       [3.21887582, 3.21887582, 0.        ]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# log(x) - 计算数组中每个元素的自然对数（以 e 为底）。\n",
    "np.log(np.square(arr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "aeb8d625-84c3-402b-a26f-cc9aa8c83ebc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2, -1, -3],\n",
       "       [-5, -5,  1]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "020e2ecd-ac00-45d2-8a5b-433dd0a5e224",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  7,  9],\n",
       "       [ 2,  1, -3]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 = np.random.randint(-5, 10, size=(2, 3))\n",
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "c061d195-30eb-4c84-875c-3276c5a3ffda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 7, 9],\n",
       "       [2, 1, 1]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对两个数组中的对应元素取最大值。\n",
    "np.maximum(arr, arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b08886a7-cfd5-44ae-ac58-aa0bf1fd6017",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2, -1, -3],\n",
       "       [-5, -5, -3]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对两个数组中的对应元素取最小值。\n",
    "np.minimum(arr, arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "70240277-49f5-49da-871d-57a44f457776",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 5],\n",
       "       [2, 1, 0]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将数组中的元素限制在给定的范围内 [a_min, a_max]。\n",
    "np.clip(arr1, 0, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f4d91419-5935-4636-ab05-728af3f5d29e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2, -1, -3],\n",
       "       [-5, -5,  1]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9594fedb-0663-4eb8-80d6-498f866826e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr[0, 0] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "183ce84a-d773-49b5-8653-c9a776f7482d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, -1, -3],\n",
       "       [-5, -5,  1]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "89d4e0e2-e94d-4da8-959a-578fc0d30a98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试数组中的所有元素是否都为真（非零）。要全部都是真才行。\n",
    "np.all(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "aa22f177-da9f-4478-9c55-92d090a54717",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0, -1, -3],\n",
       "       [-5, -5,  1]])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a6caba23-0fed-4b3a-ac12-198cecde01c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 测试数组中是否有任何元素为真（非零）。有一个是真就行了\n",
    "np.any(arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e40fc48-99ac-44a8-a953-7879fd5f33c7",
   "metadata": {},
   "source": [
    "## where函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4888d04-5927-4525-abcf-00988c85c83e",
   "metadata": {},
   "source": [
    "where函数，三个参数，条件为真时选择值的数组。条件为假时选择值的数组。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "16c32da5-cfd9-49d3-85cd-5a361e4f3112",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "arr1 = np.array([1, 3, 5, 7, 9])\n",
    "arr2 = np.array([2, 4, 6, 8, 10])\n",
    "display(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "bc92b3ef-81b0-4d50-9674-1fc806554ceb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False,  True,  True, False])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "condition = np.array([True, False, True, True, False])\n",
    "condition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "166903b6-e277-4194-97c5-46c3fa9ec235",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  5,  7, 10])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(condition, arr1, arr2)  # True的时候选arr1，False的时候选arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "510ff41f-5761-455c-bb76-861f9cbbee72",
   "metadata": {},
   "source": [
    "## 排序方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7d5d7e5-20bb-4fba-a8b1-89c417216699",
   "metadata": {},
   "source": [
    "numpy中还提供了排序方法，排序方法是就地排序，直接返回排好序的原数组。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93badfa9-c20a-49ba-9d3a-e260b051c9df",
   "metadata": {},
   "source": [
    "- arr.sort()\n",
    "- np.sort()\n",
    "- arr.argsort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "5fa376a8-7304-4b3d-b0f4-c2c13e104768",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr = np.array([9,3,11,6,17,5,4,15,1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "84b7692a-bcf9-4968-a9ab-972934f796bc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  3, 11,  6, 17,  5,  4, 15,  1])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "80b38d70-d7e8-4b38-a15f-f5108b417125",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr.sort()  # 默认升序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "34d84598-4951-4e28-92e3-a72f2aa06e09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1700f12b-d603-4901-9d06-fb2bbfb740ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "17599c52-a08a-4cb7-adaa-224e3d77b5c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "79833f6f-71a8-4db5-a650-92aa644b2975",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9,  5, 17, 15,  1,  3, 11,  6,  4])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "2a4ff118-71b1-4497-898b-475f540e4310",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 降序\n",
    "arr.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "e123c1bf-40e7-43e6-8391-a0e3dfa87bc7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([17, 15, 11,  9,  6,  5,  4,  3,  1])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "69bf2881-5b06-40df-846b-ffbb40095d04",
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "2f90ce63-f206-4f78-a497-50388d89137b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 17,  6,  5,  9,  4, 11,  3, 15])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "1965216f-70fb-4278-b12b-c75dcce99b51",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  4,  5,  6,  9, 11, 15, 17])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sort(arr)   # 如果使用np的sort方法则会返回原arr的深拷贝数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "993d5b0b-0757-49c3-854a-9addaf2b77ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1, 17,  6,  5,  9,  4, 11,  3, 15])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr  # 原数组不被改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "157508fc-25ee-4d28-ab9f-5379a0708288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 7, 5, 3, 2, 4, 6, 8, 1], dtype=int64)"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr.argsort()  # 返回排序的索引"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a23583e3-c103-409f-91d3-d5c03197b25c",
   "metadata": {},
   "source": [
    "## 集合运算函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "12cdd283-c1da-4435-ac66-95c3f6befa28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "3ab6e805-cadd-406f-98ec-53071c3373cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "1c04219d-d4f6-4251-b513-bd40fdd4e747",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([], dtype=int32)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求交集\n",
    "np.intersect1d(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "7d639589-b104-4f31-8a74-676d964b9a98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求并集\n",
    "np.union1d(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "85e515d9-2b43-4c4c-b856-14c39b63cd25",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 3, 5, 7, 9])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求差集\n",
    "np.setdiff1d(arr1, arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbb7846c-a3d8-4b12-a2e7-45984ca0443c",
   "metadata": {},
   "source": [
    "## 数学和统计函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d6128a5-6078-461f-868c-e411bdc4e819",
   "metadata": {},
   "source": [
    "- min(a) - 返回数组中的最小值。\n",
    "- max(a) - 返回数组中的最大值。\n",
    "- mean(a) - 计算数组中所有元素的平均值。\n",
    "- median(a) - 计算数组中所有元素的中位数。\n",
    "- sum(a) - 计算数组中所有元素的总和。\n",
    "- std(a) - 计算数组中所有元素的标准差。\n",
    "- var(a) - 计算数组中所有元素的方差。\n",
    "- cumsum(a) - 计算数组中元素的累积和，返回一个与输入相同形状的新数组，其中每个元素是原数组中相应位置之前所有元素的和。\n",
    "- cumprod(a) - 计算数组中元素的累积乘积，返回一个与输入相同形状的新数组，其中每个元素是原数组中相应位置之前所有元素的乘积。\n",
    "- argmin(a) - 返回数组中最小值的索引。\n",
    "- argmax(a) - 返回数组中最大值的索引。\n",
    "- argwhere(a) - 返回满足条件的元素的索引，通常与布尔数组一起使用。\n",
    "- cov(m, y=None) - 计算两组数据之间的协方差矩阵。如果只提供一个参数，则计算该参数的自协方差。\n",
    "- corrcoef(x, y=None) - 计算两组数据之间的相关系数矩阵。如果只提供一个参数，则计算该参数的自相关系数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "7146a975-4c9c-4b1a-b481-e277736a50bf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 28, 14, 19, 26, 11, 27, 22, 10, 29])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr3 = np.random.randint(10, 30, size=(10, ))\n",
    "arr3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "b67bced2-1a71-48f2-88fb-e2b196582d97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最小值\n",
    "arr3.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "4fc7dd88-3ff9-4c31-a537-a18d2dae240c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值\n",
    "arr3.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "b9901552-664f-476f-b5f1-04f3eeb48348",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最小值索引\n",
    "arr3.argmin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "6268d212-396e-4a8c-a535-5097a6c56956",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求最大值索引\n",
    "arr3.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "3ac228e4-9934-4b4a-954a-9f66fd053465",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19.7"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求平均值\n",
    "arr3.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "d089a5e0-9410-4e0b-9196-6d7cdf9faadd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20.5"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求中位数\n",
    "np.median(arr3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "643507a2-ef89-42e6-9069-671793a29762",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'numpy.ndarray' object has no attribute 'median'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[77], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43marr3\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmedian\u001b[49m()\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'median'"
     ]
    }
   ],
   "source": [
    "arr3.median()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "69a428d4-b9aa-43ed-bf95-f53e7c22be29",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "197"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求和\n",
    "arr3.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "b3c97f32-7f53-4181-832b-561e76ba0164",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 0, 4, 9],\n",
       "       [2, 6, 3, 4],\n",
       "       [3, 4, 7, 0]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4 = np.random.randint(0, 10, size=(3, 4))\n",
    "arr4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "id": "c6cc7d4c-c5fa-4ded-82c1-628e6a440ade",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "44"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr4.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "0e7f24a7-8f14-400c-8ea6-c57cd98809a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 7, 10, 14, 13])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求和，每行求和\n",
    "arr4.sum(axis=0) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "21df2df9-9c3b-4519-aa30-8590a9f999ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([15, 15, 14])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求和，每列求和\n",
    "arr4.sum(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "a1e8e9dc-6d5e-4b90-905e-fe1d7a94191d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2,  4,  6,  8, 10])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "c37654b5-274b-4146-85f0-ceb46e3ab110",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(10.)"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算协方差矩阵\n",
    "# 计算两组数据之间的协方差矩阵。如果只提供一个参数，则计算该参数的自协方差。\n",
    "np.cov(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "2eff8adc-4f21-491a-822c-24480877cd7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算两组数据之间的相关系数矩阵。\n",
    "np.corrcoef(arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "ba80bd65-2716-4f76-997b-b750f1376fd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果只提供一个参数，则计算该参数的自相关系数。\n",
    "np.corrcoef(arr1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "60a7ccfd-ae80-411a-a0c4-0c9dde55e9a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [1., 1.]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.corrcoef(arr1, arr2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "66dd35e3-badd-44b8-8123-1b6e02340ded",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
